﻿using ComLib;
using ComLib.ConfigClass;
using LogLib;
using PhotoNamingService;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace KWindow.KPageCore
{
    public class PageConfigService
    {
        #region static field and functions
        private static object _lock = new object();
        protected static PageConfigService _instance;
        public static PageConfigService Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new PageConfigService();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        private PageConfigService()
        {
            LoadConfig();
        }

        #region 页面配置选项参数

        private WorkFlowOption _workFlow = new WorkFlowOption();
        public WorkFlowOption WorkFlow
        {
            get { return _workFlow; }
        }


        /// <summary>
        /// 成果保存目录
        /// </summary>
        private OutputDirectory _outputDirs = new OutputDirectory();

        public OutputDirectory OutputDirs
        {
            get { return _outputDirs; }
        }
        #endregion

        private void LoadConfig()
        {
            if(!File.Exists(_outputDirs.ConfigFilePath))
            {
                _outputDirs.Save();
            }
            _outputDirs.Load();
            Log.WriteAsync("用户数据输出目录:" + _outputDirs.ToString());

            if (!File.Exists(_workFlow.ConfigFilePath))
            {
                _workFlow.Save();
            }
            _workFlow.Load();
            Log.WriteAsync("用户业务流程配置:" + _workFlow.ToString());
            Log.WriteAsync("用户数据输出目录:" + _outputDirs.ToString());          
        }

        /// <summary>
        /// 系统照片命名服务
        /// </summary>        
        public NamingService Naming;

        public void InitPhotoNamingService()
        {
            try
            {
                Naming = new PhotoNamingService.NamingService();
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                MessageBox.Show(ex.Message);
                throw ex;
            }
        }

        public string CreatSerialNumber(string inParams = "")
        {
            string psn = Naming.GetPhotoName(inParams);
            Log.WriteAsync("生成序列号:" + psn);
            return psn;
        }
    }    
}
